package com.othershe.nicedialog;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.TextView;

import com.othershe.nicedialog.interf.DialogClickListener;

/**
 * 模块说明：
 *
 * @author 陈浩
 * @date 2017/12/15
 */

public class ConfirmDialog extends NiceDialog {
    private DialogClickListener dialogClickListener;
    //    private String type;
    private String titleText = "";
    private static Context mContext;
    private int titleSize = 16;//标题字体大小
    private int titleColor;//标题颜色
    private int contentTextColor;//内容字体颜色
    private String cancelText = "取消";//取消按钮内容
    private int cancelTextColor;//取消按钮字体颜色
    private int cancelTextSize = 16;//取消按钮字体大小
    private String okText = "确定";//确定按钮内容
    private int okTextColor;//确定按钮字体颜色
    private int okTextSize = 16;//确定按钮字体大小
    private int contentTextSize = 15;//内容字体大小
    private String contentText = "";
    private boolean isSingle = false;//是否显示单个按钮

    public static ConfirmDialog newInstance(Context context) {
        mContext = context;
//        Bundle bundle = new Bundle();
//        bundle.putString("type", type);
        ConfirmDialog dialog = new ConfirmDialog();
        dialog.setMargin(60);
        dialog.setOutCancel(false);
//        dialog.setArguments(bundle);
        return dialog;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        Bundle bundle = getArguments();
//        if (bundle == null) {
//            return;
//        }
//        type = bundle.getString("type");
    }

    @Override
    public int intLayoutId() {
        return R.layout.confirm_layout;
    }

    public ConfirmDialog setTitleText(String titleText) {
        this.titleText = titleText;
        return this;
    }

    public ConfirmDialog setTitleText(int resId) {
        this.titleText = getString(resId);
        return this;
    }

    public ConfirmDialog isSingle(boolean isSingle) {
        this.isSingle = isSingle;
        return this;
    }

    public ConfirmDialog setContentText(int resId) {
        this.contentText = getString(resId);
        return this;
    }

    public ConfirmDialog setContentText(String resId) {
        this.contentText = resId;
        return this;
    }

    public ConfirmDialog setTitleSize(int resId) {
        this.titleSize = resId;
        return this;
    }

    public ConfirmDialog setContentTextSize(int size) {
        this.contentTextSize = size;
        return this;
    }

    public ConfirmDialog setTitleColor(int resId) {
        this.titleColor = mContext.getResources().getColor(resId);
        return this;
    }

    public ConfirmDialog setContentTextColor(int resId) {
        this.contentTextColor = mContext.getResources().getColor(resId);
        return this;
    }

    public ConfirmDialog setCancelText(String resId) {
        this.cancelText = resId;
        return this;
    }

    public ConfirmDialog setCancelTextColor(int resId) {
        this.cancelTextColor = mContext.getResources().getColor(resId);
        return this;
    }

    public ConfirmDialog setCancelTextSize(int size) {
        this.cancelTextSize = size;
        return this;
    }

    public ConfirmDialog setOkText(String resId) {
        this.okText = resId;
        return this;
    }

    public ConfirmDialog setOkTextColor(int resId) {
        this.okTextColor = mContext.getResources().getColor(resId);
        return this;
    }

    public ConfirmDialog setOkTextSize(int size) {
        this.okTextSize = size;
        return this;
    }

    @Override
    public void convertView(ViewHolder holder, final BaseNiceDialog dialog) {
        TextView titleView = holder.getView(R.id.title);
        TextView messageView = holder.getView(R.id.message);
        TextView cancelView = holder.getView(R.id.cancel);
        TextView okView = holder.getView(R.id.ok);
        View view = holder.getView(R.id.view);
        if (!titleText.equals("")) {
            titleView.setText(titleText);
        } else {
            titleView.setVisibility(View.GONE);
        }
        if (titleColor > 0)
            titleView.setTextColor(titleColor);
        if (titleSize > 0)
            titleView.setTextSize(titleSize);

        if (!contentText.equals(""))
            messageView.setText(contentText);
        if (contentTextColor > 0)
            messageView.setTextColor(contentTextColor);
        if (contentTextSize > 0)
            messageView.setTextSize(contentTextSize);
        if (!isSingle) {
            if (cancelTextSize > 0)
                cancelView.setTextSize(cancelTextSize);
            if (!cancelText.equals(""))
                cancelView.setText(cancelText);
            if (cancelTextColor > 0)
                cancelView.setTextColor(cancelTextColor);
            if (okTextSize > 0)
                okView.setTextSize(okTextSize);
            if (!okText.equals(""))
                okView.setText(okText);
            if (okTextColor > 0)
                okView.setTextColor(okTextColor);
        } else {
            cancelView.setVisibility(View.GONE);
            view.setVisibility(View.GONE);
            if (okTextSize > 0)
                okView.setTextSize(okTextSize);
            if (!okText.equals(""))
                okView.setText(okText);
            if (okTextColor > 0)
                okView.setTextColor(okTextColor);
            okView.setBackgroundResource(R.drawable.single_dialog_butotn_selecter);
        }

        holder.setOnClickListener(R.id.cancel, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialogClickListener != null)
                    dialogClickListener.setCancelOnClickListener(v, dialog);
//                dialog.dismiss();
            }
        });

        holder.setOnClickListener(R.id.ok, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialogClickListener != null)
                    dialogClickListener.setOkOnClickListener(v, dialog);
            }
        });
    }

    @Override
    public ConfirmDialog setOutCancel(boolean outCancel) {
        super.setOutCancel(outCancel);
        return this;
    }

    @Override
    public ConfirmDialog show(FragmentManager manager) {
        super.show(manager);
        return this;
    }

    public void setDialogClickListener(DialogClickListener onItemClickListener) {
        this.dialogClickListener = onItemClickListener;
    }
}
